def save_task(dict,entry_task_name,entry_task_due_date,entry_task_description,priority_var,file_path,scale_value,listbox):
    from logic.window_function import refresh
    from datetime import datetime
    import tkinter as tk
    import json
    if scale_value == 0:
        priority_var = 'low'
    elif scale_value== 1:
        priority_var = 'medium'
    elif scale_value== 2:
        priority_var = 'high'
    print(f"Saving task:{entry_task_name},due date:{entry_task_due_date},description:{entry_task_description},priority:{priority_var}")
    print(f"Listbox:{listbox}")
    task =  { 
              "task_name":entry_task_name,
              "task_priority":priority_var,
              "task_creation_date":datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
              "task_due_date":entry_task_due_date,
              "task_description":entry_task_description,
              "task_status":"no finished"
    }      
    dict[entry_task_name] = task
    import json
    with open(file_path,'w',encoding='utf-8') as file:
        json.dump(dict, file,ensure_ascii=False,indent=4)   
        
    refresh(dict,listbox) 
    listbox.insert(tk.END, f"{entry_task_name} priority:{priority_var} due to {entry_task_due_date}")
 
        
def del_task(listbox,task_dict):
    from logic.json_function import save_json
    from tkinter import messagebox
    
    selected_index = listbox.curselection()
    if not selected_index:
        messagebox.showerror('error','You should choose a task to delete')
   

    # 从列表框中获取任务名
    task_text = listbox.get(selected_index)
    task_name = task_text.split(" ")[0]  # 任务名在文本的第一部分

    
    # 从字典中删除任务

    del task_dict[task_name]
    messagebox.showinfo('info',f"任务 '{task_name}' 已从字典中删除")  # 调试信息
    
    # 保存字典到 JSON 文件
    save_json('information/task_information.json', task_dict)

    
    
    # 从列表框中删除任务
    listbox.delete(selected_index)
    
def priority_task(window, listbox, task_information_dict):
    import tkinter as tk
    from tkinter import simpledialog, messagebox,Toplevel,Label,Entry
    from datetime import datetime
    from logic.task_function import save_task  
    # 获取当前选中的任务索引
    selected_index = listbox.curselection()
    if not selected_index:
        messagebox.showwarning("Warning", "Please select a task to edit.")
        return
    
    # 获取选中的任务字符串
    selected_task_str = listbox.get(selected_index)
    
    # 解析任务字符串
    task_parts = selected_task_str.split(' priority:')
    task_name = task_parts[0]
    task_priority_str = task_parts[1].split(' due to ')[0]
    task_due_date = task_parts[1].split(' due to ')[1]

    # 找到对应的任务信息
    for key, value in task_information_dict.items():
        if value['task_name'] == task_name and value['task_priority'] == task_priority_str and value['task_due_date'] == task_due_date:
            task_key = key
            break
    else:
        messagebox.showerror("Error", "Task not found in the dictionary.")
        return

    # 创建编辑窗口
    edit_window = Toplevel(window)
    edit_window.title("Edit Task Priority")

    # 标题输入框
    Label(edit_window, text="Title:").grid(row=0, column=0, padx=5, pady=5)
    title_entry = Entry(edit_window)
    title_entry.grid(row=0, column=1, padx=5, pady=5)
    title_entry.insert(0, value['task_name'])

    # 截止日期输入框
    Label(edit_window, text="Due Date (YYYY-MM-DD HH:MM:SS):").grid(row=1, column=0, padx=5, pady=5)
    due_date_entry = Entry(edit_window)
    due_date_entry.grid(row=1, column=1, padx=5, pady=5)
    due_date_entry.insert(0, value['task_due_date'])

    # 优先级下拉菜单
    Label(edit_window, text="Priority:").grid(row=2, column=0, padx=5, pady=5)
    priority_var = tk.StringVar(value=value['task_priority'])
    priority_options = ['high', 'medium', 'low']
    priority_menu = tk.OptionMenu(edit_window, priority_var, *priority_options)
    priority_menu.grid(row=2, column=1, padx=5, pady=5)